package org.itboys.core.service.mongo.web;

import org.itboys.commons.ErrorException;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.core.common.SundayCommonConstants;
import org.itboys.core.entity.mongo.SundayTimeConfig;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Auther: 罗尧坤
 * Date: 2018/8/16
 * Description:
 * 配送时间段服务类
 */

@Service
public class SundayTimeConfigService extends BaseMongoService<SundayTimeConfig> {
    @Resource(name = "mobileDs")
    private MongoDataSource mongoDataSource;

    @Override
    protected MongoDataSource getMongoDataSource() {
        return mongoDataSource;
    }

    @Override
    protected Class<SundayTimeConfig> getEntityClass() {
        return SundayTimeConfig.class;
    }

    /**
     * 查询
     *
     * @param request
     * @return
     */
    public PageResult<SundayTimeConfig> select(HttpServletRequest request) {
        Map<String, Object> param = ParamUtil.packageMongoExactParam(request);
        param.put("isDeleted", SundayCommonConstants.NO);
        PageResult<SundayTimeConfig> result = super.pageQuery(request, param);
        return result;
    }

    /**
     * 保存或者修改
     *
     * @param id
     * @param timeConfig
     */
    public void save(Long id, SundayTimeConfig timeConfig) {
        if (LongUtil.isNotZreo(id)) {
            super.updateExceptEmpty(id, timeConfig);
        } else {
            if (super.count(new HashMap<>()) > 0) {
                throw new ErrorException("已经存在一个时间段");
            } else
                super.save(timeConfig);
        }
    }

    /**
     * 查找一个时间段
     *
     * @param id
     * @return
     */
    public SundayTimeConfig findOne(Long id) {
        SundayTimeConfig timeConfig = null;
        if (LongUtil.isNotZreo(id)) {
            timeConfig = super.getById(id);
        } else {
            timeConfig = new SundayTimeConfig();
        }
        return timeConfig;

    }

}
